//
//  RCalendarCell.m
//  Riji
//
//  Created by liujianlong on 2022/8/18.
//

#import "RCalendarCell.h"
#import "RDayCell.h"
#import "RNoteInfoViewController.h"
@interface RCalendarCell()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSDateComponents *date;

@property (nonatomic, strong) NSArray *week;
///月份里第一天是星期几
@property (nonatomic, assign) int dayweek;
///一个月有多少天
@property (nonatomic, assign) int daymonth;

@end
@implementation RCalendarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.week = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
    [self.collectionView registerNib:[RDayCell nib] forCellWithReuseIdentifier:[RDayCell ID]];
}
- (void)setStamp:(int)stamp{
    _stamp = stamp;
    self.daymonth = (int)[NSDate numOfMonth:self.stamp];
    
    NSString *string = [NSString stringWithFormat:@"%d/%d/1",(int)self.date.year, (int)self.date.month];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    f.timeZone = [NSTimeZone localTimeZone];
    f.dateFormat = @"yyyy/MM/dd";
    NSDate *date = [f dateFromString:string];
    self.dayweek = (int)[NSDate weakDay:date];
    
    [self.collectionView reloadData];
}
- (NSDateComponents *)date{
    return [NSDate dateOfStamp:self.stamp];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.week.count + self.dayweek - 1 + self.daymonth;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RDayCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RDayCell ID] forIndexPath:indexPath];
    cell.content.text = @"";
    cell.point.hidden = YES;
    if (indexPath.row < 7) {
        cell.content.text = LS(self.week[indexPath.row]);
        cell.content.textHexColor = @"#999999";
    }else if (indexPath.row >= self.week.count + self.dayweek - 1){
        cell.content.text = [NSString stringWithFormat:@"%d",(int)(indexPath.row + 2 - self.week.count - self.dayweek)];
        cell.content.textHexColor = @"#333333";
        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"year"),bg_sqlValue(@(self.date.year)),bg_sqlKey(@"month"),bg_sqlValue(@(self.date.month)),bg_sqlKey(@"day"),bg_sqlValue(cell.content.text),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
        cell.point.hidden = [RNoteModel bg_count:nil where:where] <= 0;
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.week.count + self.dayweek - 1) {
        return;
    }else{
        RDayCell *cell = (RDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
        if (cell.point.hidden) {return;}
        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"year"),bg_sqlValue(@(self.date.year)),bg_sqlKey(@"month"),bg_sqlValue(@(self.date.month)),bg_sqlKey(@"day"),bg_sqlValue(cell.content.text),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
        [RNoteModel bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
            dispatch_async(dispatch_get_main_queue(), ^{
                RNoteInfoViewController *vc = [RNoteInfoViewController new];
                vc.type = NotesTypeNormal;
                vc.index = 0;
                vc.array = [NSMutableArray arrayWithArray:array];
                [[self currentVC].navigationController pushViewController:vc animated:YES];
            });
        }];
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake((SCREEN_WIDTH - 32) / 7, 40);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 16, 0, 16);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
@end
